import { createRouter, createWebHistory } from "vue-router";
import Index from "../views/index.vue";
import { useSysStore } from "../store";

const routes = [
    {
        path: "/",
        name: "root",
        component: Index,
        children: [
            {
                path: "/",
                redirect: "/sys/dept",
            },
        ],
    },
    {
        path: "/login",
        name: "login",
        component: () => import("../views/sys/login/index.vue"),
        meta: {
            public: true,
        },
    },
    {
        path: "/404",
        name: "404",
        component: () => import("../views/sys/404.vue"),
        meta: {
            public: true,
        },
    },
];

const router = createRouter({
    routes,
    history: createWebHistory(),
});

router.beforeEach((to, from, next) => {
    let path = to.fullPath;
    if (path == "/login") {
        return next();
    }

    // 其它情况，需要检查是否有用户信息
    const store = useSysStore();
    let userInfo = store.getUserInfo();
    if (!userInfo.id) {
        return next("/login");
    }

    return next();
});
export default router;
